package com.shangyueshang.model;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.shangyueshang.serializer.DeserializerBigDecimal;
import com.shangyueshang.serializer.SerializerBigDecimal;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.YearMonth;
import java.util.Optional;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/19 14:24
 * @version: v1.0.0
 */
@Data
public class ShopRevenue implements Serializable {
	
	/**
	 * 店铺
	 */
	private String shop;
	
	/**
	 * 统计月份
	 */
	private YearMonth tradeMonth;
	
	/**
	 * 销售收入总额
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal productSales;
	
	/**
	 * 销售数量
	 */
	private Integer saleQuantity;
	
	/**
	 * 销售毛收入，单位：元
	 * 已剔除佣金，FBA配送费、税费等
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal saleRevenue;
	
	/**
	 * 佣金费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal sellingFee;
	
	/**
	 * FBA配送费，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal fbaFee;
	
	/**
	 * 采购费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal purchaseFee;
	
	/**
	 * 头程运费，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal headwayFee;
	
	/**
	 * 包装费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal packFee;
	
	/**
	 * 退款费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal returnFee;
	
	/**
	 * 退款数量
	 */
	private Integer returnQuantity;
	
	/**
	 * 广告费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal advertiseFee;
	
	/**
	 * 店铺租金，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal subscriptionFee;
	
	/**
	 * 促销活动费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal dealFee;
	
	/**
	 * 优惠券手续费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal couponRedemptionFee;
	
	/**
	 * 库存配置费，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal fbaInboundPlacementServiceFee;
	
	/**
	 * 入库缺陷费，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal inboundDefectFee;
	
	/**
	 * AMZ邀评费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal earlyReviewerFee;
	
	/**
	 * FBA库存管理费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal fbaInventoryFee;
	
	/**
	 * 物流库存报销费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal adjustmentFee;
	
	/**
	 * 债务费用，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal debtFee;
	
	/**
	 * 物流赔偿费用调整，记为收入，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal adjustmentCompensate;
	
	/**
	 * AWD模式费用，记为收入，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal awdFee;
	
	/**
	 * Send模式费用，记为收入，单位：元
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	@JsonDeserialize(using = DeserializerBigDecimal.class)
	private BigDecimal sendShippingCharge;
	
	/**
	 * 毛利润，单位：元
	 *
	 * @return
	 */
	@JsonSerialize(using = SerializerBigDecimal.class)
	public BigDecimal getSaleProfit() {
		return Optional.ofNullable(saleRevenue).orElse(BigDecimal.ZERO)
				.add(Optional.ofNullable(purchaseFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(headwayFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(packFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(returnFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(advertiseFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(subscriptionFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(dealFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(couponRedemptionFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(fbaInboundPlacementServiceFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(inboundDefectFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(earlyReviewerFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(fbaInventoryFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(adjustmentFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(debtFee).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(adjustmentCompensate).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(awdFee).orElse(BigDecimal.ZERO));
				// 计算预估头程计算，不需要计算Send运输费，该费用项在公司成本核算
				//.add(Optional.ofNullable(sendShippingCharge).orElse(BigDecimal.ZERO));
	}
	
	/**
	 * FBA费用占比
	 *
	 * @return
	 */
	public String getFbaProportion() {
		return this.fbaFee
				.abs()
				.multiply(BigDecimal.valueOf(100L))
				.divide(Optional.ofNullable(productSales).orElse(BigDecimal.ONE), 2, RoundingMode.HALF_UP)
				.toString()
				+ "%";
	}
	
	/**
	 * 广告费用占比
	 *
	 * @return
	 */
	public String getAdvertiseProportion() {
		return this.advertiseFee
				.abs()
				.multiply(BigDecimal.valueOf(100L))
				.divide(Optional.ofNullable(productSales).orElse(BigDecimal.ONE), 2, RoundingMode.HALF_UP)
				.toString()
				+ "%";
	}
}
